package com.poetry.vup.user.Interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.poetry.vup.common.exception.KabobException;
import com.poetry.vup.common.exception.KabobExceptionUtil;
import com.poetry.vup.common.redis.RedisStringService;
import com.poetry.vup.common.result.BaseResult;
import com.poetry.vup.common.result.ResultCode;
import com.poetry.vup.common.util.JacksonUtil;
import com.poetry.vup.common.util.jwt.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录拦截器
 */
@Component
@Slf4j
public class AuthorizationInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private RedisStringService redisStringService;


    /*@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("Authorization");
        Map<String, Object> map = new HashMap<>();
        if(StringUtils.isBlank(token)){
            log.info("token不能为空");
            map.put(String.valueOf(ResultCode.RESOURCE_NOT_FOUND.getCode()),"无token令牌");
            String jsonString = JSON.toJSONString(map);
            renderJson(response, jsonString);
            return false;
            //response.sendRedirect(request.getContextPath() + "/vupUser/login");
        }else{
            String uid = jwtTokenUtil.getUserToken(token);
            if(!redisStringService.hasKey(uid)){
                log.info("暂未登录或token已经过期");
                //KabobExceptionUtil.throwError(ResultCode.UNAUTHORIZED,ResultCode.UNAUTHORIZED.getMessage());
                map.put(String.valueOf(ResultCode.UNAUTHORIZED.getCode()),ResultCode.UNAUTHORIZED.getMessage());
                String jsonString = JSON.toJSONString(map);
                renderJson(response, jsonString);
                return false;
                //response.sendRedirect(request.getContextPath() + "/vupUser/login");
            }
        }
        return true;
    }*/

    private void renderJson(HttpServletResponse response,String data){
        response.setContentType("application/json");

        response.setCharacterEncoding("UTF-8");

        try {
            PrintWriter writer = response.getWriter();
            writer.println(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
